package ar.edu.utn.frgp.Banco.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import ar.edu.utn.frgp.Banco.dao.IDao;
import ar.edu.utn.frgp.Banco.model.EntityBase;

abstract class BaseDao<T extends EntityBase> implements IDao<T> {

	protected HibernateTemplate hibernateTemplate;

	public void create(T obj) {
		hibernateTemplate.save(obj);
	}

	public void delete(T entity) {
		hibernateTemplate.delete(entity);
	}
	
	public void update(T entity) {
		hibernateTemplate.update(entity);
	}

	@SuppressWarnings("unchecked")
	public List<T> findByExample(T obj) {
		return hibernateTemplate.findByExample(obj);
	}

	@SuppressWarnings("unchecked")
	protected List<T> getAll(Class<?> clazz) {
		return (List<T>) hibernateTemplate.loadAll(clazz);
	}
	

	@SuppressWarnings("unchecked")
	public T getById(T entity) {

		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		T ret = (T) session.get(entity.getClass(), entity.getId());
		//session.close();
		return ret;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	
}
